package com.itheima.config;

import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//全局异常处理器
@Slf4j
@RestControllerAdvice
public class ProjectExceptionAdvice {
    //具体的异常处理
    /*//业务异常
    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        System.err.println("异常处理器接受到了异常！");
        ex.printStackTrace();
        System.err.println("以上为该异常的信息");
        return new Result(0,ex.getMessage());
    }
    //系统异常
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        System.err.println("异常处理器接受到了异常！");
        ex.printStackTrace();
        System.err.println("以上为该异常的信息");
        return new Result(0,ex.getMessage());
    }*/
    //其他异常
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R doOtherException(SQLIntegrityConstraintViolationException ex){
        log.info(ex.getMessage());
        if (ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split(" ");
            return R.error(split[2]+"用户已存在");
        }
        return R.error("未知错误");
    }
}
